package com.zstu.mall.common;

import com.zstu.mall.entity.pojo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * create by: wuyc
 * description: BaseServiceImpl
 * create time: 2019/12/2 18:46
 */
//@Transactional
public abstract class BaseServiceImpl<T extends BaseEntity, ID extends Serializable, R extends BaseRepository<T, ID>>
        implements BaseService<T, ID, R> {

    @PersistenceContext
    protected EntityManager em;

    @Autowired
    protected R baseRepository;

    @Override
    public T get(ID id) {
        System.out.println(this.baseRepository.getOne(id).toString());
        T entity =  this.baseRepository.getOne(id);
        return entity;
    }

    @Override
    public String createId(){
        String id;
        Random random = new Random();
        do{
            id = "";
            id = id + String.valueOf(random.nextInt(9)+1);
            for(int i=0;i<6;i++){
                id = id + random.nextInt(10);
            }
            //如果6位满足则跳出
            if(this.baseRepository.existsById((ID) id) == false)
                break;
            id = id + random.nextInt(100000000);
        }while (this.baseRepository.existsById((ID) id) == true);
        return id;
    }

    @Override
    public Page<T> findByPage(Pageable pageable) {
        return this.baseRepository.findAll(pageable);
    }

    @Override
    public List<T> findAll(){
        return this.baseRepository.findAll();
    }

    @Override
    public ResultBean<T> delete(ID id) {
        //by id的手动移除
        this.baseRepository.deleteById(id);
        return new ResultBean();
    }

    @Override
    public ResultBean<T> delete(T entity) {
        this.baseRepository.delete(entity);
        return new ResultBean(entity);
    }

    @Override
    public ResultBean<T> create(T entity) {
        System.out.println(baseRepository+"");
        if(Objects.isNull(entity)){
            return new ResultBean("数据为空,无法创建!");
        }
        if(this.exists((ID) entity.getId())){
            return new ResultBean("实体id相同,无法重复创建!");
        }
        System.out.println(entity.toString());
        entity = this.baseRepository.saveAndFlush(entity);
        if(Objects.isNull(entity)){
            return new ResultBean("数据为空,无法创建!");
        }
        return new ResultBean(entity);
    }

    @Override
    public ResultBean<T> update(T entity) {
        if(Objects.isNull(entity)){
            return new ResultBean("数据为空,无法创建!");
        }
        if(!this.exists((ID) entity.getId())){
            return new ResultBean("数据库不存在该数据,无法执行更新");
        }
        //添加修改时间
        entity.setGmtModified(new Date());
        entity = this.baseRepository.saveAndFlush(entity);
        if(Objects.isNull(entity)){
            return new ResultBean("数据为空,无法创建!");
        }
        return new ResultBean(entity);
    }

    @Override
    public boolean exists(ID id) {
        return this.baseRepository.existsById(id);
    }

}

